package com.taotao.tools.enums;

import com.taotao.tools.core.str.StringUtil;
import com.taotao.tools.enums.annotation.EnumId;
import com.taotao.tools.enums.annotation.EnumVersion;
import com.taotao.tools.enums.utils.BaseEnumUtils;
import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner;
import io.github.lukehutch.fastclasspathscanner.scanner.ScanResult;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 * 枚举集中管理
 * </p>
 *
 * @author youbeiwuhuan
 * @version 1.0.0
 * @date 2022/4/2 14:39
 */
@Slf4j
public class EnumHub {
    private static ConcurrentHashMap<String, Class<? extends BaseEnum>> currEnumMap = new ConcurrentHashMap<>();

    private static ConcurrentHashMap<String, Set<Class<? extends BaseEnum>>> enumMap = new ConcurrentHashMap<>();


    private static EnumHub instance;

    public static EnumHub getInstance() {

        if (null == instance) {
            throw new IllegalStateException("EnumHub 还未初始化，请调用EnumHub.getInstance(String enumPackages) 进行初始化");
        }

        return instance;
    }


    /**
     * 多个包用逗号隔开
     *
     * @param enumPackages
     */
    public static EnumHub getInstance(String enumPackages) {
        if (StringUtil.isBlank(enumPackages)) {
            throw new IllegalArgumentException("enumPackages is blank");
        }

        if (null == instance) {
            synchronized (EnumHub.class) {
                if (null == instance) {
                    instance = new EnumHub(enumPackages);
                    instance.init();
                }
            }
        } else {
            if (!Objects.equals(enumPackages, instance.enumPackages.trim())) {
                throw new IllegalStateException("EnumHub has been  init by other enumPackages!!");
            }
        }
        return instance;
    }

    private String enumPackages;

    /**
     * 多个包用逗号隔开
     *
     * @param enumPackages
     */
    private EnumHub(String enumPackages) {
        this.enumPackages = enumPackages;
    }


    public void init() {
        initEnumMap(enumPackages);
    }

    private synchronized void initEnumMap(String enumPackages) {
        Set<Class<? extends BaseEnum>> restClasses = new HashSet<>();
        ScanResult result = new FastClasspathScanner(enumPackages.split(",")).matchClassesImplementing(BaseEnum.class, e -> restClasses.add(e)).scan();
        if (null != restClasses && restClasses.isEmpty()) {
            return;
        }

        for (Class<? extends BaseEnum> ec : restClasses) {

            EnumId ei = ec.getAnnotation(EnumId.class);

            Class<? extends BaseEnum> old = currEnumMap.get(ei.value());
            if (null != old) {
                int oldVersion = getEnumVersion(old);
                int currVersion = getEnumVersion(ec);


                if (currVersion == oldVersion) {
                    throw new IllegalStateException("Duplex enum version for enumcode [" + ei.value() + "], one enum is " + old.getName() + ",other  enum  is" + ec.getName());
                }

                if (currVersion > oldVersion) {
                    currEnumMap.put(ei.value(), ec);
                }


            } else {
                currEnumMap.put(ei.value(), ec);
            }


        }
    }

    private int getEnumVersion(Class<? extends BaseEnum> old) {
        EnumVersion oldEnumVersion = old.getAnnotation(EnumVersion.class);
        if (null != oldEnumVersion) {
            return oldEnumVersion.value();
        }

        return 0;
    }

    public static Class<? extends BaseEnum> getEnumClass(String enumId) {
        return currEnumMap.get(enumId);
    }


    /**
     * 给下拉框用
     *
     * @param enumId
     * @return
     */
    public static List<BaseEnumView> getCodeTextEnumViewList(String enumId) {
        Class<? extends BaseEnum> ec = currEnumMap.get(enumId);
        if (null == ec) {
            return Collections.EMPTY_LIST;
        }


        return BaseEnumUtils.getCodeTextEnumViewList(ec);
    }


}
